home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libimp / impzoom.c.z / impzoom.c
C/C++ Source or Header  |  1996-05-06  |  7KB  |  291 lines

  1. /**************************************************************************
  2.  *
  3.  *           Copyright (c)    1993 Silicon Graphics, Inc.
  4.  *            All Rights Reserved
  5.  *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  7.  *
  8.  * The copyright notice above does not evidence any actual of intended
  9.  * publication of such source code, and is an unpublished work by Silicon
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly
  13.  * prohibited.
  14.  *
  15.  * RESTRICTED RIGHTS LEGEND:
  16.  *
  17.  * Use, duplication or disclosure by the Government is subject to
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311
  24.  **************************************************************************
  25.  *
  26.  * File: impzoom.c
  27.  *
  28.  * Description: Zooms an SGI Image file. The type of zooming can be
  29.  *    specified. The default is impulse zooming (i.e. replicative/
  30.  *    decimation).
  31.  *
  32.  *    Usage: impzoom [-f filter] [-b blur] [-l] xfact yfact inimage outimage
  33.  *
  34.  *    If -l is specified the xfact and yfact are interpreted as the
  35.  *    actual size of the zoomed image in pixels. The default is to
  36.  *    treat xfact and yfact as zoom factors.
  37.  *
  38.  **************************************************************************/
  39.  
  40.  
  41. #ident "$Revision: 1.3 $"
  42.  
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <sys/types.h>
  48. #include "imp.h"
  49.  
  50.  
  51. /* Row read function client data structure */
  52.  
  53. typedef struct {
  54.     int channel;
  55.     IMPImage *image;
  56. } RowInfo;
  57.  
  58.  
  59. /* Local functions */
  60.  
  61. static void usage(char *pname);
  62. static int readRow(short *buffer, ushort_t row, void *clientData);
  63.  
  64.  
  65. /**************************************************************************
  66.  *
  67.  * Function: main
  68.  *
  69.  * Description: Program entry point
  70.  *
  71.  * Parameters: 
  72.  *    argc (I) - number of command line arguments
  73.  *    argv (I) - command line arguments
  74.  *
  75.  * Return: 0 if no errors. 1 if errors.
  76.  *
  77.  **************************************************************************/
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.     char *pname, *inName, *outName;
  82.     char *xfactptr, *yfactptr;
  83.     int ch, inNumChan, literal = 0;
  84.     register int row;
  85.     ushort_t srcXSize, srcYSize, dstXSize, dstYSize;
  86.     short *buffer;
  87.     IMPFilterType filterType = IMPImpulse;
  88.     IMPImage *inImage, *outImage;
  89.     float blur = 1.0;
  90.     IMPZoom *zoom;
  91.     RowInfo rowInfo;
  92.  
  93.     /*
  94.      * Extract the program name
  95.      */
  96.     pname = ((pname = strrchr(argv[0], '/')) == NULL) ? argv[0]: pname + 1;
  97.  
  98.     /*
  99.      * Handle command line options
  100.      */
  101.     while ((ch = getopt(argc, argv, "f:b:l")) != EOF) {
  102.     switch (ch) {
  103.         case 'f':
  104.         if (!strcasecmp(optarg, "IMPULSE"))
  105.             filterType = IMPImpulse;
  106.         else if (!strcasecmp(optarg, "BOX"))
  107.             filterType = IMPBox;
  108.         else if (!strcasecmp(optarg, "TRIANGLE"))
  109.             filterType = IMPTriangle;
  110.         else if (!strcasecmp(optarg, "QUADRATIC"))
  111.             filterType = IMPQuadratic;
  112.         else if (!strcasecmp(optarg, "MITCHELL"))
  113.             filterType = IMPMitchell;
  114.         else if (!strcasecmp(optarg, "GAUSSIAN"))
  115.             filterType = IMPGaussian;
  116.         else
  117.             fprintf(stderr,
  118.             "%s: unrecognized filter type %s, IMPULSE used\n",
  119.             pname, optarg);
  120.         break;
  121.         case 'b':
  122.         blur = atof(optarg);
  123.         break;
  124.         case 'l':
  125.         literal = 1;
  126.         break;
  127.         case '?':
  128.         default:
  129.         usage(pname);
  130.         exit(1);
  131.     }
  132.     }
  133.  
  134.     /*
  135.      * Handle required command line arguments
  136.      */
  137.     if (argc - optind < 4) {
  138.     usage(pname);
  139.     exit(1);
  140.     }
  141.     xfactptr = argv[optind++];
  142.     yfactptr = argv[optind++];
  143.     inName = argv[optind++];
  144.     outName = argv[optind];
  145.  
  146.     /*
  147.      * Open the original image
  148.      */
  149.     if ((inImage = impOpen(inName, "r")) == NULL ) {
  150.     impPerror(pname);
  151.     exit(1);
  152.     }
  153.     srcXSize = impXSize(inImage);
  154.     srcYSize = impYSize(inImage);
  155.     inNumChan = impNumChannels(inImage);
  156.  
  157.     /*
  158.      * Determine the zoomed image dimensions
  159.      */
  160.     if (literal) {
  161.     dstXSize = atoi(xfactptr);
  162.     dstYSize = atoi(yfactptr);
  163.     } else {
  164.     dstXSize = srcXSize * atof(xfactptr) + 0.5;
  165.     dstYSize = srcYSize * atof(yfactptr) + 0.5;
  166.     }
  167.     if (!dstXSize || !dstYSize) {
  168.         fprintf(stderr, "%s: zoom size %d by %d is invalid\n",
  169.             pname, dstXSize, dstYSize);
  170.     exit(1);
  171.     }
  172.  
  173.     /*
  174.      * Open the zoomed image
  175.      */
  176.     if ((outImage = impOpen(outName, "w",
  177.         impMakeRasterType(impRasterEncoding(inImage),
  178.                  impRasterBPP(inImage)),
  179.         impDimension(inImage),
  180.         dstXSize, dstYSize, inNumChan,
  181.         impImageType(inImage),
  182.         impName(inImage))) == NULL) {
  183.     impPerror(pname);
  184.     exit(1);
  185.     }
  186.  
  187.     /*
  188.      * Create the zoom operator
  189.      */
  190.     if ((zoom = impCreateZoom(srcXSize, srcYSize, dstXSize, dstYSize,
  191.                   impMinValue(inImage), impMaxValue(inImage),
  192.                   readRow, 1, filterType, blur)) == NULL) {
  193.     impPerror(pname);
  194.     exit(1);
  195.     }
  196.  
  197.     /*
  198.      * Zoom the original image
  199.      */
  200.     buffer = (short*)malloc(dstXSize * sizeof(short));
  201.     rowInfo.image = inImage;
  202.     for (rowInfo.channel = 0; rowInfo.channel < inNumChan; rowInfo.channel++) {
  203.     /*
  204.      * Reset the zoom operator between channels
  205.      */
  206.     impResetZoom(zoom);
  207.  
  208.     for (row = 0; row < dstYSize; row++) {
  209.             if (impZoomRow(zoom, buffer, row, &rowInfo) < 0) {
  210.         impPerror(pname);
  211.         exit(1);
  212.             }
  213.             if (impWriteRow(outImage, buffer, row, rowInfo.channel) < 0) {
  214.         impPerror(pname);
  215.         exit(1);
  216.             }
  217.     }
  218.     }
  219.     free(buffer);
  220.  
  221.     /*
  222.      * Destroy the zoom operator
  223.      */
  224.     impDestroyZoom(zoom);
  225.  
  226.     /*
  227.      * Close up all images and free storage
  228.      */
  229.     if (impClose(inImage) < 0) {
  230.     impPerror(pname);
  231.     exit(1);
  232.     }
  233.     if (impClose(outImage) < 0) {
  234.     impPerror(pname);
  235.     exit(1);
  236.     }
  237.  
  238.     return 0;
  239. }
  240.  
  241.  
  242. /*
  243.  =======================================================================
  244.             LOCAL FUNCTIONS
  245.  =======================================================================
  246. */
  247.  
  248.  
  249. /**************************************************************************
  250.  *
  251.  * Function: usage
  252.  *
  253.  * Description: Prints program usage message.
  254.  *
  255.  * Parameters: 
  256.  *    pname (I) - program name
  257.  *
  258.  * Return: none
  259.  *
  260.  **************************************************************************/
  261.  
  262. static void usage(char *pname)
  263. {
  264.     fprintf(stderr,
  265.     "Usage: %s [-f filter] [-b blur] [-l] xfact yfact inimage outimage\n",
  266.     pname);
  267. }
  268.  
  269.  
  270. /**************************************************************************
  271.  *
  272.  * Function: readRow
  273.  *
  274.  * Description: Called by the zooming function to read a row of data.
  275.  *
  276.  * Parameters: 
  277.  *    buffer (I) - buffer to fill with row data
  278.  *    row (I) - row number to read
  279.  *    clientData (I) - pointer to client data.
  280.  *
  281.  * Return: -1 if errors, 0 or greater if no errors.
  282.  *
  283.  **************************************************************************/
  284.  
  285. static int readRow(short *buffer, ushort_t row, void *clientData)
  286. {
  287.     register RowInfo *rowInfo = (RowInfo*)clientData;
  288.  
  289.     return impReadRow(rowInfo->image, buffer, row, rowInfo->channel);
  290. }
  291.